package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallKeyword;
import com.ctsi.ssdc.domain.LitemallKeywordExample;
import com.ctsi.ssdc.service.LitemallKeywordService;
import com.ctsi.ssdc.repository.LitemallKeywordRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallKeyword.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallKeywordServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallKeywordRepository, LitemallKeyword, Integer, LitemallKeywordExample> 
	implements LitemallKeywordService {

    @Autowired
    LitemallKeywordRepository litemallKeywordRepository;


    /**
    * GET  /litemallKeywords : get the litemallKeywords firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallKeyword> findFirstStringColumn(String keyword,Pageable pageable){
        String str = keyword;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallKeywordExample litemallKeywordExample = new LitemallKeywordExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallKeywordExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallKeywordExample.createCriteria();
        } else{
            litemallKeywordExample.createCriteria().andKeywordLike("%" + str +"%");
        }
        List<LitemallKeyword>  data = litemallKeywordRepository.selectByExample(litemallKeywordExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallKeywordRepository.countByExample(litemallKeywordExample);
        }
        return new PageResult<LitemallKeyword>(data,count,count);
    }

    @Override
    public LitemallKeyword queryDefault() {
        LitemallKeywordExample example = new LitemallKeywordExample();
        example.or().andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        return litemallKeywordRepository.selectOneByExample(example);
    }

    @Override
    public List<LitemallKeyword> queryHots() {
        LitemallKeywordExample example = new LitemallKeywordExample();
        example.or().andIsHotEqualTo(true).andDeletedEqualTo(false);
        return litemallKeywordRepository.selectByExample(example);
    }

    @Override
    public List<LitemallKeyword> queryByKeyword(String keyword, Integer page, Integer limit) {
        LitemallKeywordExample example = new LitemallKeywordExample();
        example.setDistinct(true);
        example.or().andKeywordLike("%" + keyword + "%").andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        return litemallKeywordRepository.selectByExampleSelective(example, LitemallKeyword.Column.keyword);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
